package testview.zhen.com.myapplication;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.provider.AlarmClock;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


import java.lang.ref.WeakReference;
import java.util.ArrayList;

public class TestNotificationActivity extends AppCompatActivity {
    Thread th;
    private static final String TAG = "MainActivity";
    private static class MyHander extends Handler{

        private final WeakReference<TestNotificationActivity> mActivity  ;
        public MyHander(TestNotificationActivity activity){
            mActivity = new WeakReference<TestNotificationActivity>(activity);

        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
           if (null != mActivity.get()) {
                switch (msg.what){
                    case 1:
                        mActivity.get().toNotify();
                        break;
                        default:
                            break;
                }
           }
        }
    }

    MyHander myHander;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHander = new MyHander(this);
        Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message = myHander.obtainMessage();
                message.what = 1;
                message.obj = getString(R.string.text);
                myHander.sendMessageDelayed(message,5000);

                myHander.post(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }
        });
    }


    public void toNotify() {
        //大概步骤 获取服务 设置通道 （设置渠道 将渠道放入服务中 设置通道到渠道中）（可有可无）
        //发送通知
        //1 先获取 通知的服务
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        //2 Android8.0以上的兼容
        if (Build.VERSION_CODES.O <= Build.VERSION.SDK_INT){
            //3 NotificationChannel设置各种信息
            //兼容Android8.0以上的通知信息
            NotificationChannel channel = new NotificationChannel(getString(R.string.notify),"名字",NotificationManager.IMPORTANCE_HIGH);
            channel.enableLights(true);// 开启指示灯，如果设备有的话
            channel.setLightColor(ContextCompat.getColor(getApplication(), R.color.colorAccent));// 设置指示灯颜色
            channel.setShowBadge(true);// 是否在久按桌面图标时显示此渠道的通知
            channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PRIVATE);// 设置是否应在锁定屏幕上显示此频道的通知
            channel.setBypassDnd(true);// 设置绕过免打扰模式

            manager.createNotificationChannel(channel);
            //4 增加不同的渠道
            //设置类别的渠道 设置两组通道 一种是普通消息提醒，另外一种是下载服务消息提醒
            ArrayList<NotificationChannelGroup> groups = new ArrayList<>();

            NotificationChannelGroup group = new NotificationChannelGroup("1","正常提示的Group名字");
            groups.add(group);

            NotificationChannelGroup group_download= new NotificationChannelGroup("2","下载的GroupDown名字");
            groups.add(group_download);
            //5将渠道添加到NotificationManager中
            manager.createNotificationChannelGroups(groups);

            //6 将NotificationChannel设置到某个渠道中
            channel.setGroup("1");
            Notification builder = new NotificationCompat.Builder(TestNotificationActivity.this,"正常提示")
                    .setTicker("状态栏标题")//设置状态栏标题
                    .setContentTitle("标题位置")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText("状态栏弹出信息的内容")
                    .setNumber(1).build();
                    manager.notify(1,builder);
//                    manager.notify(builder);
        }
    }

    public void startTimer(String message, int seconds) {
        Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER)
                .putExtra(AlarmClock.EXTRA_MESSAGE, message)
                .putExtra(AlarmClock.EXTRA_LENGTH, seconds)
                .putExtra(AlarmClock.EXTRA_SKIP_UI, true);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    /*
     *启动设备已安装的app
     */
    public void openTargetApp(String packageName) {
        //包管理器对象
        PackageManager pm = getPackageManager();
        //检查包名
        PackageInfo packageInfo = null;
        try {
            packageInfo = pm.getPackageInfo(packageName, 0);
        }
        catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        //包名存在则启动app
        if (packageInfo != null) {
            Intent intent = pm.getLaunchIntentForPackage(packageName);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            this.startActivity(intent);
        }
        else {
            Toast.makeText(TestNotificationActivity.this, "应用程序未安装", Toast.LENGTH_LONG).show();
        }
    }

}
